﻿using DocumentFormat.OpenXml.Bibliography;
using DocumentFormat.OpenXml.Wordprocessing;
using Domain.Factory_Modeling;
using Domain.Material_Management;
using Domain.Process_Procedures;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Production_Modules;
using MES.Server.API.Dto.Warehouse;
using Microsoft.EntityFrameworkCore;
using ReturnValue;
using System.Diagnostics;
using System.Reflection.Metadata;
using System.Security.Claims;
namespace MES.Server.API.Application.Handler.Production_Modules
{
    /// <summary>
    /// 工单显示
    /// </summary>
    public class ProduceQueryCommandHandler : IRequestHandler<ProduceQueryCommand, ApiPaging<ProduceDTO>>
    {
        IRepository<ClientModel> ClientRep;
        IRepository<MaterialGroupModel> GrouplRep;
        /// <summary>
        /// 工艺表
        /// </summary>
        IRepository<ProcessFlowModel> ProcessFlowRep;
        /// <summary>
        ///产线
        /// </summary>
        IRepository<ProductionLineModel> ProductionLineRep;
        /// <summary>
        /// 生产工艺
        /// </summary>
        IRepository<ProduceModel> ProduceRep;
        /// <summary>
        /// 商品表
        /// </summary>
        IRepository<OrderModel> OrderRep;
        IRepository<WarehouseModel> Warehouse;
        IRepository<WorkShopModel> WorkShopRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ClientRep"></param>
        /// <param name="GrouplRep"></param>
        /// <param name="ProcessFlowRep"></param>
        /// <param name="ProductionLineRep"></param>
        /// /// <param name="ProduceRep"></param>
        public ProduceQueryCommandHandler(IRepository<ClientModel> ClientRep, IRepository<MaterialGroupModel> GrouplRep, IRepository<ProcessFlowModel> ProcessFlowRep, IRepository<ProductionLineModel> ProductionLineRep, IRepository<ProduceModel> ProduceRep, IRepository<OrderModel> OrderRep, IRepository<WarehouseModel> Warehouse, IRepository<WorkShopModel> WorkShopRep)
        {
            this.ClientRep = ClientRep;
            this.GrouplRep = GrouplRep;
            this.ProcessFlowRep = ProcessFlowRep;
            this.ProductionLineRep = ProductionLineRep;
            this.ProduceRep = ProduceRep;
            this.OrderRep = OrderRep;
            this.Warehouse = Warehouse;
            this.WorkShopRep = WorkShopRep;
        }

        public async Task<ApiPaging<ProduceDTO>> Handle(ProduceQueryCommand request, CancellationToken cancellationToken)
        {
            var list = from a in ProduceRep.GetAll()
                       join b in ProductionLineRep.GetAll() on a.ProductionLineId equals b.Id
                       join c in GrouplRep.GetAll() on a.MaterialGroupId equals c.Id
                       join d in ProcessFlowRep.GetAll() on a.ProcessFlowId equals d.Id
                       join e in ClientRep.GetAll() on a.ClientId equals e.Id
                       join f in OrderRep.GetAll() on a.OrderId equals f.Id
                       join j in WorkShopRep.GetAll() on a.WorkShopId equals j.Id
                       join h in Warehouse.GetAll() on a.WarehouseId equals h.Id
                       where !a.IsDel
                       select new ProduceDTO
                       {
                           Id = a.Id,
                           ProduceCode=a.ProduceCode,
                           ProduceType=a.ProduceType,
                           ProduceName = a.ProduceName,
                           ClientId=a.ClientId,
                           MaterialGroupId=a.MaterialGroupId,
                           WarehouseId=a.WarehouseId,
                           ProcessFlowId=a.ProcessFlowId,
                           ProductionLineId = a.ProductionLineId,
                           FinishProduceNum=a.FinishProduceNum,
                           StartDate = a.StartDate,
                           EntDate = a.EntDate,
                           ProduceNum=a.ProduceNum,
                           Ischeck=a.Ischeck,
                           FlowCode= d.FlowCode,
                           FlowName=d.FlowName,
                           ProcessCount=d.ProcessCount,
                           PersonId=d.PersonId,
                           DepartmentId=d.DepartmentId,
                           IsActive=d.IsActive,
                           Remarks=d.Remarks,
                           ClientCode = e.ClientCode,
                           ClientName=e.ClientName,
                           ClientTel=e.ClientTel,
                           ClientNickname=e.ClientNickname,
                           ClientAddr=e.ClientAddr,
                           ClientSocial=e.ClientSocial,
                           ClientDesc = e.ClientDesc,
                           MaterialGroupCode = c.MaterialGroupCode,
                           MaterialGroupDesc = c.MaterialGroupDesc,
                           MaterialGroupName=c.MaterialGroupName,
                           MaterialGroupType=c.MaterialGroupType,
                           ProductionLineCode=b.ProductionLineCode,
                           ProductionLineName=b.ProductionLineName,
                           WorkShopId=b.WorkShopId,
                           ProductionLineDesc=b.ProductionLineDesc,
                           OrderId=a.OrderId,
                           OrderCode=f.OrderCode,
                           OrderSon=f.OrderSon,
                           OrderNum=f.OrderNum,
                           OrderState=f.OrderState,
                           OrderGoods=f.OrderGoods,
                           IsOrder=f.IsOrder,
                           WarehouseCode=h.WarehouseCode,
                            WarehouseName=h.WarehouseName,
                            WarehouseNum=h.WarehouseNum,
                            WarehouseIdSon=h.WarehouseIdSon,
                            WarehouseAddr=h.WarehouseAddr,
                            WarehouseArea=h.WarehouseArea,
                            WarehouseDesc=h.WarehouseDesc,
                            WorkShopCode=j.WorkShopCode,
                            WorkShopDesc=j.WorkShopDesc,
                            WorkShopName=j.WorkShopName,
                           CreaterPeople = a.CreaterPeople,
                            CreateTime=a.CreateTime,
                            UpdaterPeople=a.UpdaterPeople,
                            UpdateTime=a.UpdateTime,


                       };
            int totalcount = await list.CountAsync();
            int totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.Size);
            ApiPaging<ProduceDTO> ping = new ApiPaging<ProduceDTO>();
            ping.Code = StatusCode.Success;
            ping.Message = "查询成功";
            ping.Totalcount = totalcount;
            ping.TotalPage = await list.OrderByDescending(x => x.CreateTime).Skip((request.Index - 1) * request.Size).Take(request.Size).ToListAsync();
            return ping;

        }
    }
}
